--[[-------------------------------------------------------------------------
  Copyright (c) 2007, Trond A Ekseth
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of oMirrorBars nor the names of its contributors
        may be used to endorse or promote products derived from this
        software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------]]
-- oMirrorBars - core/mirror.lua

local G = getfenv(0)
local name, db, profile = "oMirrorBars"
local defaults = {
	profile = {
		width = 200,
		height = 25,
		textSize = 11,
		backdrop = 'shade',

		pos = {
			["BREATH"] = "TOP#UIParent#TOP#0#-96",
			["EXHAUSTION"] = "TOP#UIParent#TOP#0#-119",
			["FEIGNDEATH"] = "TOP#UIParent#TOP#0#-142",
		},
	},
}

local addon = DongleStub('Dongle-1.0'):New(name)
addon.frames = {}
local frames = addon.frames

function addon:Enable()
	db = self:InitializeDB"oMBDB"
	db:RegisterDefaults(defaults)

	profile = db.profile
	self.profile = db.profile

	UIParent:UnregisterEvent"MIRROR_TIMER_START"

	self:RegisterEvent"MEETINGSTONE_CHANGED"
	self:RegisterEvent"MIRROR_TIMER_START"
	self:RegisterEvent"MIRROR_TIMER_STOP"

	-- TODO: Figure out exactly when this information is avaible.
	local type, value, maxvalue, scale, paused, text
	for i=1,MIRRORTIMER_NUMTIMERS do
		type, value, maxvalue, scale, paused, text = GetMirrorTimerInfo(i)
		if(type ~= "UNKNOWN") then
			frames[type] = self.bars:new(type)
			frames[type]:start(value, maxvalue, scale, paused, text)
		end
	end

	self.cmd = self:InitializeSlashCommand("oMirrorBars Slash Commands", name, "omb", "omirrorbars")
	self.cmd:RegisterSlashHandler("|cff33ff99width|r: Set the width of the timers.", "^width%s?(.-)$", "setWidth")
	self.cmd:RegisterSlashHandler("|cff33ff99height|r: Set the height of the timers.", "^height%s?(.-)$", "setHeight")
	self.cmd:RegisterSlashHandler("|cff33ff99size|r: Set the text size on the timers.", "^size%s?(.-)$", "setTextSize")
	self.cmd:RegisterSlashHandler("|cff33ff99unlock|r: Unlock all frames.", "^unlock$", "unlock")
	self.cmd:RegisterSlashHandler("|cff33ff99lock|r: Lock all frames.", "^lock$", "lock")
	self.cmd:RegisterSlashHandler("|cffff5555reset|r: Reset the frame position to default.", "^reset$", "resetPosition")
end

-- cmd's... this just screams that I should write a common handler...
-- ... but it's only temporary.

function addon:setWidth(width)
	local value = tonumber(width)

	if(not value and type(width) == "string") then
		if(#width == 0) then
			return self:Print("Usage: width <value>. Example: width 200")
		elseif(width == "default") then
			width = defaults.profile.width
		else
			return self:Print("Usage: width <value>. Example: width 200")
		end
	end

	for k, v in pairs(frames) do
		v:SetWidth(width)
	end

	profile.width = width
	self:PrintF("Width has been set to: %s", width)
end

function addon:setHeight(height)
	local value = tonumber(height)

	if(not value and type(height) == "string") then
		if(#height == 0) then
			return self:Print("Usage: height <value>. Example: height 25")
		elseif(height == "default") then
			height = defaults.profile.height
		else
			return self:Print("Usage: height <value>. Example: height 25")
		end
	end

	for k, v in pairs(frames) do
		v:SetHeight(height)
	end

	profile.height = height
	self:PrintF("Height has been set to: %s", height)
end

function addon:setTextSize(ts)
	local value = tonumber(ts)

	if(not value and type(ts) == "string") then
		if(#ts == 0) then
			return self:Print("Usage: size <value>. Example: ts 1")
		elseif(ts == "default") then
			ts = defaults.profile.textSize
		else
			return self:Print("Usage: size <value>. Example: ts 11")
		end
	end

	for k, v in pairs(frames) do
		v.text:SetFont(STANDARD_TEXT_FONT, ts)
	end

	profile.textSize = ts
	self:PrintF("Text size has been set to: %s", ts)
end

function addon:resetPosition()
	for k, v in pairs(frames) do
		local p1, parent, p2, x, y = self:loadPosition(k, 1)

		v:ClearAllPoints()
		v:SetPoint(p1, parent, p2, x, y)
		self:savePosition(k)
	end
end

function addon:unlock()
	for k in pairs(defaults.profile.pos) do
		local frame = frames[k] or self.bars:new(k)
		frame.paused = 1
		frame.text:SetText(k:lower())
		frame:Show()
	end
end

function addon:lock()
	for k in pairs(defaults.profile.pos) do
		local frame = frames[k] or self.bars:new(k)
		frame.paused = nil
		frame:Hide()
	end
end

-- events

function addon:MIRROR_TIMER_START(event, type, value, maxvalue, scale, paused, text)
	local frame = frames[type] or self.bars:new(type)

	frame:start(value, maxvalue, scale, paused, text)
end

function addon:MIRROR_TIMER_STOP(event, type)
	local frame = frames[type] or self.bars:new(type)

	frame:stop()
end

-- this is madness

function addon:savePosition(id)
	local p1, frame, p2, x, y = frames[id]:GetPoint()
	local sep = "#"
	local scale = frames[id]:GetEffectiveScale()

	if(frame == nil) then frame = "UIParent" end
	if(type(frame) == "table") then frame = frame:GetName() end

	profile.pos[id] = p1..sep..frame..sep..p2..sep..x..sep..y
end

function addon:loadPosition(id, force)
	local pos = (force and defaults.profile.pos[id]) or profile.pos[id]
	local p1, frame, p2, x, y = strsplit("#", pos)

	return p1, frame, p2, x, y
end

G[name] = addon
